package edu.isistan.matching.filters;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 * Filtro compuesto, representa la union de los filtros asi como el procesamiento en pipe de los mismos
 * @author Mariano
 *
 */
public class ComposedFilter<IN,OUT> implements IMatchingFilter<IN,OUT> {

	/**
	 * Conjunto de filtros conectados
	 */
	private List<IMatchingFilter> filters;

	public ComposedFilter() {
		super();
		this.filters = new Vector<IMatchingFilter>();
	}

	public void addFilter(IMatchingFilter filter) {
		this.filters.add(filter);
	}

	public void removeFilter(IMatchingFilter filter) {
		this.filters.remove(filter);
	}

	public OUT process(IN parameter) {
		Iterator<IMatchingFilter> itFilters = this.filters.iterator();
		Object result = parameter;
		while (itFilters.hasNext()) {
			result = itFilters.next().process(result);
		}
		return (OUT) result;
	}

	public IMatchingFilter getFilter(Class filterClass) {
		for(IMatchingFilter filter: filters) {
			if (filter.getClass().equals(filterClass)) {
				return filter;
			}
		}
		return null;
	}
}
